package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotate.AuotFill;
import com.sky.annotate.DropRedis;
import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Transactional(rollbackFor = Exception.class)
    @DropRedis
    @Override
    public Result AddCategory(Category category) {
        categoryMapper.AddCategory(category);
        return Result.success();
    }

    @Override
    public Result GetPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> categories = categoryMapper.GetPage(categoryPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setRecords(categories.getResult());
        pageResult.setTotal(categories.getTotal());
        return Result.success(pageResult);
    }

    @Override
    @Transactional
    public Result DeleteById(Integer id) {
        long id1 = Long.parseLong(id + "");
        Category category = categoryMapper.getById(id1);
        if (category.getType() ==1){
            List<DishVO> dishVOSList = dishMapper.getByCategoryId(id1);
            if (dishVOSList.size() >0){
                return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }

        }else {
            List<Setmeal> setmealList = setmealMapper.getByCategoryId(id1);
            if (setmealList.size() >0){
                return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
            }
        }
        categoryMapper.DeleteById(id);
        return Result.success();
    }

    @Transactional
    @Override
    @DropRedis
    public Result Update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        if (category.getSort() ==null){
            category = category.toBuilder().status(1).build();
        }
        categoryMapper.Update(category);
        return Result.success();
    }

    @Transactional
    @Override
    @DropRedis
    public Result Update(Integer status, Integer id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(Long.parseLong(id+""));
        categoryMapper.Update(category);
        return Result.success();
    }

    // 获取分类
    @Override
    public Result getType(Integer type) {
        List<Category> categories = categoryMapper.getType(type);

        return Result.success(categories);

    }
}
